package com.hm.health.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goodix.utils.ToastUtils;
import com.hm.health.R;
import com.hm.health.entity.BleDevicesInfo;

import java.util.List;

/**
 * Created by chenshi on 2016/11/21.
 */
public class LeDevicesAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    // 设备列表
    private List<BleDevicesInfo> mBluetoothList;
    // 是否为多选
    private boolean mIsSingleSelected = false;
    private List<BleDevicesInfo> mSelectedDevices;


    public LeDevicesAdapter(Context c, List<BleDevicesInfo> devices,
                            List<BleDevicesInfo> selectedDevices, boolean bSingleSelect) {
        mContext = c;
        mBluetoothList = devices;
        mLayoutInflater = LayoutInflater.from(c);
        this.mIsSingleSelected = bSingleSelect;
        mSelectedDevices = selectedDevices;
    }

    @Override
    public int getCount() {
        if (null != mBluetoothList) {
            return mBluetoothList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (null != mBluetoothList && mBluetoothList.size() > position) {
            mBluetoothList.get(position);
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.layout_le_device_item, null);
            viewHolder = new ViewHolder();
            viewHolder.mTvName = convertView.findViewById(R.id.tv_name);
            viewHolder.mTvAddress = convertView.findViewById(R.id.tv_address);
            viewHolder.mImgCheck = convertView.findViewById(R.id.img_check);
            viewHolder.mTvRSSI = convertView.findViewById(R.id.tv_rssi);
            viewHolder.mRelativeLayout = convertView.findViewById(R.id.rl_root);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (position < mBluetoothList.size()) {
            BleDevicesInfo device = mBluetoothList.get(position);
            if (null != device) {
                viewHolder.mTvRSSI.setText(String.valueOf(device.getRSSI()));
                if (device.getDeviceName() != null && !device.getDeviceName().isEmpty()) {
                    String description = device.getDeviceName();
                    if (device.getDeviceName().compareTo(device.getDeviceName()) != 0) {
                        description = device.getDeviceName() + "(" + device.getDeviceName() + ")";
                    }
                    viewHolder.mTvName.setText(description);
                } else {
                    viewHolder.mTvName.setText(mContext.getResources().getString(R.string.unknown_device));
                }

                viewHolder.mTvAddress.setText(device.getDeviceAddress());
                int pos = mSelectedDevices.indexOf(device);
                int nVisibility = pos >= 0 ? View.VISIBLE : View.INVISIBLE;
                viewHolder.mImgCheck.setVisibility(nVisibility);
            }
        }

        viewHolder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick(view, position);
            }
        });

        return convertView;
    }


    public void onItemClick(View view, int position) {
        if (position >= mBluetoothList.size()) {
            return;
        }

        if (mIsSingleSelected) {
            if (mSelectedDevices.contains(mBluetoothList.get(position))) {
                mSelectedDevices.clear();
            } else {
                mSelectedDevices.clear();
                mSelectedDevices.add(mBluetoothList.get(position));
            }
        } else {
            BleDevicesInfo devicesInfo = mBluetoothList.get(position);
            int index = mSelectedDevices.indexOf(devicesInfo);
            if (index >= 0) {
                mSelectedDevices.remove(index);
            } else {
                if (mSelectedDevices.size() >= 4) {
                    ToastUtils.showLongToastMessage(mContext, "Select up to 4");
                    return;
                }
                mSelectedDevices.add(devicesInfo);
            }
        }

        notifyDataSetChanged();
    }

    public static class ViewHolder {
        public TextView  mTvName;
        public TextView  mTvAddress;
        public ImageView mImgCheck;
        public TextView  mTvRSSI;
        public RelativeLayout mRelativeLayout;
    }


}
